var e = getApp();

Page({
    data: {
        swlist: [],
        code_url: [],
        code_data_img: []
    },
    onLoad: function(e) {
        e.id && this.setData({
            id: e.id
        }), this.getData();
    },
    name: function(e) {
        var a = e.detail.value;
        this.setData({
            "info.code_name": a
        });
    },
    guide: function(e) {
        var a = e.detail.value;
        this.setData({
            "info.code_guide": a
        });
    },
    service_guide: function(e) {
        var a = e.detail.value;
        this.setData({
            "info.customer_service_guide": a
        });
    },
    change: function(e) {
        console.log(e);
        var a = e.detail.value;
        this.setData({
            "info.explain": a,
            "info.explain_content": ""
        });
    },
    change1: function(e) {
        var a = e.detail.value;
        this.setData({
            "info.recommend": a
        });
    },
    onRead: function() {
        var a = this, t = e.getNetAddresss("upload.uploadPic");
        wx.chooseImage({
            count: 1,
            sizeType: [ "original", "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(e) {
                var o = e.tempFilePaths;
                wx.compressImage({
                    src: o[0],
                    quality: 60,
                    success: function(e) {
                        wx.showLoading({
                            title: "上传中"
                        });
                        var o = e.tempFilePath;
                        wx.uploadFile({
                            url: t,
                            filePath: o,
                            name: "file",
                            success: function(e) {
                                wx.hideLoading({
                                    success: function(e) {}
                                });
                                var t = JSON.parse(e.data);
                                1 == t.result ? (console.log(o, t.data.img), a.setData({
                                    imageUrl: t.data.img_url,
                                    "info.customer_service": t.data.img
                                })) : wx.showToast({
                                    icon: "none",
                                    title: t.msg,
                                    duration: 1500
                                });
                            }
                        });
                    },
                    fail: function(e) {},
                    complete: function(e) {}
                });
            }
        });
    },
    onRead_2: function() {
        var e = this;
        wx.chooseImage({
            count: 9,
            sizeType: [ "original", "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(a) {
                console.log(a.tempFilePaths), e.unload({
                    tempFilePaths: a.tempFilePaths
                }, "1");
            }
        });
    },
    unload: function(a, t) {
        console.log(t), wx.showLoading({
            title: "上传中"
        });
        var o = e.getNetAddresss("upload.uploadPic"), i = this, s = a.i ? a.i : 0, n = a.success ? a.success : 0, c = a.fail ? a.fail : 0;
        wx.uploadFile({
            url: o,
            filePath: a.tempFilePaths[s],
            name: "file",
            formData: null,
            success: function(e) {
                wx.hideLoading({
                    success: function(e) {}
                });
                var a = JSON.parse(e.data);
                console.log(a.data.img_url, a.data.img), "1" == t ? (i.data.code_data_img.push({
                    code_url: a.data.img
                }), i.data.code_data.push({
                    code_url: a.data.img_url
                }), console.log(i.data.code_data), i.setData({
                    code_data: i.data.code_data
                })) : 2 == t && (i.data.code_data.push({
                    code_url: a.data.img_url
                }), console.log(i.data.code_data), i.setData({
                    code_data: i.data.code_data
                }));
            },
            fail: function(e) {
                wx.hideLoading({
                    success: function(e) {}
                }), c++;
            },
            complete: function(e) {
                wx.hideLoading({
                    success: function(e) {}
                }), ++s == a.tempFilePaths.length ? (console.log("执行完毕"), console.log("成功：" + n + " 失败：" + c)) : (a.i = s, 
                a.success = n, a.fail = c, i.unload(a, t));
            }
        });
    },
    delImg: function(e) {
        var a = e.currentTarget.dataset.ind, t = this.data.code_data, o = this.data.code_data_img;
        o.splice(a, 1), t.splice(a, 1), console.log(t, "截取后"), this.setData({
            code_data: t,
            code_data_img: o
        });
    },
    upperLimit: function(e) {
        var a = e.detail.value, t = e.currentTarget.dataset.ind;
        this.data.code_data[t].upper_limit = a;
    },
    explaincon: function(e) {
        var a = e.detail.value;
        this.setData({
            "info.explain_content": a
        });
    },
    onReady: function() {},
    onShow: function() {
        this.getset();
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    save: function() {
        var a = this.data.info, t = this.data.code_data;
        if (0 !== t.length) {
            for (var o = 0; o < t.length; o++) "upper_limit" in t[o] == 0 && wx.showToast({
                title: "请输入扫码上限人数",
                icon: "none",
                duration: 1e3
            });
            var i = {
                id: Number(this.data.id),
                code_name: a.code_name,
                code_guide: a.code_guide,
                explain: a.explain ? "1" : "0",
                customer_service: a.customer_service,
                customer_service_guide: a.customer_service_guide,
                code_data: JSON.stringify(this.data.code_data_img)
            };
            a.explain && (i.explain_content = a.explain_content, e._isTextEmpty(a.explain_content)) ? wx.showToast({
                title: "请输入说明内容"
            }) : (a.recommend && (i.recommend = a.recommend ? "1" : "0"), this.saveSubmit(i));
        } else wx.showToast({
            title: "请上传群二维码",
            icon: "none",
            duration: 1e3
        });
    },
    getData: function() {
        var a = this, t = e.getNetAddresss("plugin.group-code.api.group-code.detail");
        e._postNetWork({
            url: t,
            data: {
                id: a.data.id
            },
            success: function(e) {
                var t = e.data;
                if (1 == t.result) {
                    console.log(t);
                    for (var o = [], i = 0; i < t.data.data.has_many_code.length; i++) o.push({
                        code_url: t.data.data.has_many_code[i].code_url_img,
                        upper_limit: t.data.data.has_many_code[i].upper_limit,
                        id: t.data.data.has_many_code[i].id
                    });
                    a.setData({
                        info: t.data.data,
                        code_data: o,
                        imageUrl: t.data.data.customer_service_img
                    }), console.log(a.data.code_data);
                } else wx.showToast({
                    title: t.msg,
                    icon: "none"
                }), console.log(t.msg);
            }
        });
    },
    getset: function() {
        var a = this, t = e.getNetAddresss("plugin.group-code.api.set.index");
        e._postNetWork({
            url: t,
            success: function(e) {
                var t = e.data;
                1 == t.result ? (console.log(t), a.setData({
                    is_open: t.data.set.is_open,
                    auth_recommend: t.data.set.auth_recommend
                })) : (wx.showToast({
                    title: t.msg,
                    icon: "none"
                }), console.log(t.msg));
            }
        });
    },
    saveSubmit: function(a) {
        var t = e.getNetAddresss("plugin.group-code.api.group-code.update");
        e._postNetWork({
            url: t,
            data: a,
            success: function(e) {
                var a = e.data;
                1 == a.result ? (console.log(a), wx.showToast({
                    title: a.msg,
                    icon: "none",
                    duration: 1e3,
                    success: function(e) {
                        setTimeout(function() {
                            wx.navigateBack({
                                delta: 1
                            });
                        }, 1500);
                    }
                })) : (wx.showToast({
                    title: a.msg,
                    icon: "none"
                }), console.log(a.msg));
            }
        });
    }
});